package GameState.Ships;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import GameState.Ships.PirateShip;


/**
 * 
 * Renders a pirate ship
 * 
 * @author Tom
 *
 */
public class PirateShipRenderer {
	
	private final static String SHIP_IMAGE_PATH = "pirateShip.png";
	private final static String ANCHOR_IMAGE_PATH = "anchor.jpg";	
	
	private final BufferedImage mShipImage;
	private final BufferedImage mAnchorImage;
		
	
	/**
	 * 
	 * @throws IOException
	 */
	public PirateShipRenderer() throws IOException{
		
			mShipImage = ImageIO.read( new File(SHIP_IMAGE_PATH));
			mAnchorImage = ImageIO.read( new File( ANCHOR_IMAGE_PATH ));
		
	}

	/**
	 * 
	 * @param ship
	 * @return
	 */
	public BufferedImage renderShip(PirateShip ship)
	{
			int direction = ship.getDirection();
			int GRAPHICS_HEIGHT = mShipImage.getHeight(); 
			int GRAPHICS_WIDTH = mShipImage.getHeight();
	
			BufferedImage shipImage = new BufferedImage(GRAPHICS_WIDTH,	GRAPHICS_HEIGHT, BufferedImage.TYPE_INT_ARGB);
			Graphics2D shipGraphics = (Graphics2D)shipImage.getGraphics();
	
			//Draws in the anchor
			if(ship.getAnchorOn()){
				//draws on to the image
				shipGraphics.drawImage(mAnchorImage, 0, 0, null);
			}
	
			if( ship.isGhostShip()){
				shipGraphics.setComposite(makeComposite(0.75f));
			}
	
			//translate the image to the correct place
			shipGraphics.translate(GRAPHICS_WIDTH/2 - mShipImage.getWidth() /2, GRAPHICS_HEIGHT/2 - mShipImage.getHeight()/ 2);
	
			//rotate the image to the correct place
			shipGraphics.rotate(Math.toRadians(direction),mShipImage.getWidth() /2 , mShipImage.getHeight() /2);
	
			//draws on to the image
			shipGraphics.drawImage(mShipImage, 0, 0, null);
	
			return shipImage;

	
		
		
		
	}

	
	/**
	 * 
	 * Makes the ship see through
	 * 
	 * @param alpha
	 * @return
	 */
	private AlphaComposite makeComposite(float alpha) {
		int type = AlphaComposite.SRC_OVER;
		return(AlphaComposite.getInstance(type, alpha));
	}

}
